package com.apes.fn.rpt.profit.grossProfitProcess.processor;

import com.apes.fn.rpt.profit.grossProfitProcess.CommonGrossProfitProcess;
import com.apes.fn.rpt.profit.grossProfitProcess.GrossProfitProcess;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 会员储值/定金
 */
@Component
public class VipCardPaymentExpense extends CommonGrossProfitProcess implements GrossProfitProcess {

    @Override
    public String getSQL(Date taskDate){

        return " SELECT TRUNC(X.CREATE_DATE)               CREATE_DATE, " +
                "             '预收'                               INCOME_CATEGORIES, " +
                "             '会员储值/订金' " +
                "                                                SERVE_TYPE_NAME, " +
                "             X.DEPT_ID, " +
                "             X.COMPANY_ID, " +
                "             SUM(X.PRICE_TOTAL)                 AMOUNT, " +
                "             0                                  TIMES, " +
                "             SUM(X.PRICE_TOTAL) " +
                "                                                PRICE_TOTAL, " +
                "             0                                  COST, " +
                "             0                                  CONSUMABLE_USE," +
                "0 vip_in,0 vip_out, " +
                "             0                                  ACCESSORY_COST, " +
                "             0                                  MAN_HOUR_COST, " +
                "             SUM(X.PLATFORM_MANAGEMENT_FEE) " +
                "                                                PLATFORM_MANAGEMENT_FEE, " +
                "             0                                  SUPPLY_CHAIN_MANAGEMENT_FEE" +
                "      FROM (SELECT TRUNC(A.DATE_APPROVE) CREATE_DATE, " +
                "                   A.ID, " +
                "                   B.PRICE_TOTAL, " +
                "                   A.DEPT_ID, " +
                "                   A.COMPANY_ID, " +
                "                   B.PLATFORM_MANAGEMENT_FEE " +
                "            FROM RETAIL_ORDER A, " +
                "                 RETAIL_ORDER_ITEM B " +
                "            WHERE A.ID = B.SALE_ID " +
                "              AND A.DATE_APPROVE >= to_date(:P_DATE, 'YYYY-mm-dd') " +
                "              AND A.DATE_APPROVE " +
                "                < to_date(:P_DATE, 'YYYY-mm-dd') + 1 " +
                "              AND EXISTS(SELECT F.ID " +
                "                         FROM MAS_PRODUCT E, " +
                "                              MAS_SERVE_TYPE F, " +
                "                              MAS_SERVE_CATEGORY G " +
                "                         WHERE E.CLASSIFY_ID = G.CLASSIFY_ID " +
                "                           AND F.ID = G.SERVE_TYPE_ID " +
                "                           AND B.PRODUCT_ID = E.ID " +
                "                           AND F.ID = '012') " +
                "            UNION ALL " +
                "            SELECT TRUNC(A.DATE_APPROVE) CREATE_DATE, " +
                "                   A.ID, " +
                "                   B.PRICE_TOTAL, " +
                "                   A.DEPT_ID, " +
                "                   A.COMPANY_ID, " +
                "                   B.PLATFORM_MANAGEMENT_FEE " +
                "            FROM ADVANCE_PAYMENT A, " +
                "                 ADVANCE_PAYMENT_ITEM B " +
                "            WHERE A.ID = B.ADVANCE_PAYMENT_ID " +
                "              AND A.DATE_APPROVE >= to_date(:P_DATE, 'YYYY-mm-dd') " +
                "              AND A.DATE_APPROVE < to_date(:P_DATE, 'YYYY-mm-dd') + 1) X " +
                "      GROUP BY X.CREATE_DATE, X.DEPT_ID, " +
                "               X.COMPANY_ID";
    }

    @Override
    public boolean support(String type) {
        return "VipCardPayment".equals(type);
    }
}
